/*action create by pionner code builder*/
package com.pioneer.app.sms.define;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.Action;
import com.pioneer.app.KeyValue;
import com.pioneer.app.dev.osuser.user.Userlist;
import com.pioneer.app.dev.osuser.user.UserlistDAO;

public class TypeDefineAction implements Action,ServletRequestAware, Serializable {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(TypeDefineAction.class);
	public static final String TYPE_SQL="数据库";
	public static final String TYPE_MOBILE="导入接受人";
	// key
	private java.lang.Integer id;

	private java.lang.Integer[] ids;
	private TypeDefine typedefine;
	private java.lang.String title;
	private java.lang.String content;
	private java.lang.String sqlstr;
	private java.lang.String contentFrom;
	private java.lang.String startDate;
	private java.lang.Integer loopType;
	private java.lang.String typeName;
	private java.lang.String dbLocation;
	private java.lang.String dbType;
	private java.lang.String dbDirver;
	private java.lang.String dbUser;
	private java.lang.String dbPassword;
	private java.lang.String status;
	private String tmpl;
	private String mobilefilename;
	private String toMobiles;
	private String toPersons;
	private File mobilefile;
	private List mobiles=new ArrayList();
	private HttpServletRequest request;
	private List objects=null;
	private String condition=null;
	private List statuslist=new ArrayList();
	
	
	/** Ĭ�Ϲ��췽�� */
	public TypeDefineAction(){
		statuslist.add(new KeyValue("启动","1"));
		statuslist.add(new KeyValue("关闭","0"));
	}
	/** full constructor */
	public TypeDefineAction(java.lang.String content,java.lang.String sqlstr,java.lang.String contentFrom,java.lang.String startDate,java.lang.Integer loopType,java.lang.String typeName,java.lang.String dbLocation,java.lang.String dbType,java.lang.String dbDirver,java.lang.String dbUser,java.lang.String dbPassword,java.lang.String statu){
		this.content=content;
		this.sqlstr=sqlstr;
		this.contentFrom=contentFrom;
		this.startDate=startDate;
		this.loopType=loopType;
		this.typeName=typeName;
		this.dbLocation=dbLocation;
		this.dbType=dbType;
		this.dbDirver=dbDirver;
		this.dbUser=dbUser;
		this.dbPassword=dbPassword;
		this.status=status;

	}
	public void setId(java.lang.Integer id){
		if (logger.isDebugEnabled()) {
			logger.debug("setId(java.lang.Integer) - start");
		}
		this.id=id;

		if (logger.isDebugEnabled()) {
			logger.debug("setId(java.lang.Integer) - end");
		}
	}
	public java.lang.Integer getId(){
		if (logger.isDebugEnabled()) {
			logger.debug("getId() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getId() - end");
		}
		return id;
	}

	public void setIds(java.lang.Integer[] ids){
		if (logger.isDebugEnabled()) {
			logger.debug("setIds(java.lang.Integer[]) - start");
		}
		this.ids=ids;

		if (logger.isDebugEnabled()) {
			logger.debug("setIds(java.lang.Integer[]) - end");
		}
	}
	public java.lang.Integer[] getIds(){
		if (logger.isDebugEnabled()) {
			logger.debug("getIds() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getIds() - end");
		}
		return ids;
	}

	public void setTypeDefine(TypeDefine typedefine){
		if (logger.isDebugEnabled()) {
			logger.debug("setTypeDefine(TypeDefine) - start");
		}
		this.typedefine=typedefine;

		if (logger.isDebugEnabled()) {
			logger.debug("setTypeDefine(TypeDefine) - end");
		}
	}
	public TypeDefine getTypeDefine(){
		if (logger.isDebugEnabled()) {
			logger.debug("getTypeDefine() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getTypeDefine() - end");
		}
		return typedefine;
	}

	public void setContent(java.lang.String content){
		if (logger.isDebugEnabled()) {
			logger.debug("setContent(java.lang.String) - start");
		}
		this.content=content;

		if (logger.isDebugEnabled()) {
			logger.debug("setContent(java.lang.String) - end");
		}
	}
	public java.lang.String getContent(){
		if (logger.isDebugEnabled()) {
			logger.debug("getContent() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getContent() - end");
		}
		return content;
	}

	public void setSqlstr(java.lang.String sqlstr){
		if (logger.isDebugEnabled()) {
			logger.debug("setSqlstr(java.lang.String) - start");
		}
		this.sqlstr=sqlstr;

		if (logger.isDebugEnabled()) {
			logger.debug("setSqlstr(java.lang.String) - end");
		}
	}
	public java.lang.String getSqlstr(){
		if (logger.isDebugEnabled()) {
			logger.debug("getSqlstr() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getSqlstr() - end");
		}
		return sqlstr;
	}

	public void setContentFrom(java.lang.String contentFrom){
		if (logger.isDebugEnabled()) {
			logger.debug("setContentFrom(java.lang.String) - start");
		}
		this.contentFrom=contentFrom;

		if (logger.isDebugEnabled()) {
			logger.debug("setContentFrom(java.lang.String) - end");
		}
	}
	public java.lang.String getContentFrom(){
		if (logger.isDebugEnabled()) {
			logger.debug("getContentFrom() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getContentFrom() - end");
		}
		return contentFrom;
	}

	public void setStartDate(java.lang.String startDate){
		if (logger.isDebugEnabled()) {
			logger.debug("setStartDate(java.lang.String) - start");
		}
		this.startDate=startDate;

		if (logger.isDebugEnabled()) {
			logger.debug("setStartDate(java.lang.String) - end");
		}
	}
	public java.lang.String getStartDate(){
		if (logger.isDebugEnabled()) {
			logger.debug("getStartDate() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getStartDate() - end");
		}
		return startDate;
	}

	public void setLoopType(java.lang.Integer loopType){
		if (logger.isDebugEnabled()) {
			logger.debug("setLoopType(java.lang.Integer) - start");
		}
		this.loopType=loopType;

		if (logger.isDebugEnabled()) {
			logger.debug("setLoopType(java.lang.Integer) - end");
		}
	}
	public java.lang.Integer getLoopType(){
		if (logger.isDebugEnabled()) {
			logger.debug("getLoopType() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getLoopType() - end");
		}
		return loopType;
	}

	public void setTypeName(java.lang.String typeName){
		if (logger.isDebugEnabled()) {
			logger.debug("setTypeName(java.lang.String) - start");
		}
		this.typeName=typeName;

		if (logger.isDebugEnabled()) {
			logger.debug("setTypeName(java.lang.String) - end");
		}
	}
	public java.lang.String getTypeName(){
		if (logger.isDebugEnabled()) {
			logger.debug("getTypeName() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getTypeName() - end");
		}
		return typeName;
	}

	public void setDbLocation(java.lang.String dbLocation){
		if (logger.isDebugEnabled()) {
			logger.debug("setDbLocation(java.lang.String) - start");
		}
		this.dbLocation=dbLocation;

		if (logger.isDebugEnabled()) {
			logger.debug("setDbLocation(java.lang.String) - end");
		}
	}
	public java.lang.String getDbLocation(){
		if (logger.isDebugEnabled()) {
			logger.debug("getDbLocation() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getDbLocation() - end");
		}
		return dbLocation;
	}

	public void setDbType(java.lang.String dbType){
		if (logger.isDebugEnabled()) {
			logger.debug("setDbType(java.lang.String) - start");
		}
		this.dbType=dbType;

		if (logger.isDebugEnabled()) {
			logger.debug("setDbType(java.lang.String) - end");
		}
	}
	public java.lang.String getDbType(){
		if (logger.isDebugEnabled()) {
			logger.debug("getDbType() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getDbType() - end");
		}
		return dbType;
	}

	public void setDbDirver(java.lang.String dbDirver){
		if (logger.isDebugEnabled()) {
			logger.debug("setDbDirver(java.lang.String) - start");
		}
		this.dbDirver=dbDirver;

		if (logger.isDebugEnabled()) {
			logger.debug("setDbDirver(java.lang.String) - end");
		}
	}
	public java.lang.String getDbDirver(){
		if (logger.isDebugEnabled()) {
			logger.debug("getDbDirver() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getDbDirver() - end");
		}
		return dbDirver;
	}

	public void setDbUser(java.lang.String dbUser){
		if (logger.isDebugEnabled()) {
			logger.debug("setDbUser(java.lang.String) - start");
		}
		this.dbUser=dbUser;

		if (logger.isDebugEnabled()) {
			logger.debug("setDbUser(java.lang.String) - end");
		}
	}
	public java.lang.String getDbUser(){
		if (logger.isDebugEnabled()) {
			logger.debug("getDbUser() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getDbUser() - end");
		}
		return dbUser;
	}

	public void setDbPassword(java.lang.String dbPassword){
		if (logger.isDebugEnabled()) {
			logger.debug("setDbPassword(java.lang.String) - start");
		}
		this.dbPassword=dbPassword;

		if (logger.isDebugEnabled()) {
			logger.debug("setDbPassword(java.lang.String) - end");
		}
	}
	public java.lang.String getDbPassword(){
		if (logger.isDebugEnabled()) {
			logger.debug("getDbPassword() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getDbPassword() - end");
		}
		return dbPassword;
	}

	public void setStatus(java.lang.String status){
		if (logger.isDebugEnabled()) {
			logger.debug("setStatus(java.lang.String) - start");
		}
		this.status=status;

		if (logger.isDebugEnabled()) {
			logger.debug("setStatus(java.lang.String) - end");
		}
	}
	public java.lang.String getStatus(){
		if (logger.isDebugEnabled()) {
			logger.debug("getStatus() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getStatus() - end");
		}
		return status;
	}

	public List getObjects() {
		if (logger.isDebugEnabled()) {
			logger.debug("getObjects() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getObjects() - end");
		}
		return objects;
	}

	public void setObjects(List objects) {
		if (logger.isDebugEnabled()) {
			logger.debug("setObjects(List) - start");
		}
		this.objects = objects;

		if (logger.isDebugEnabled()) {
			logger.debug("setObjects(List) - end");
		}
	}

	public String getCondition() {
		if (logger.isDebugEnabled()) {
			logger.debug("getCondition() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getCondition() - end");
		}
		return condition;
	}

	public void setCondition(String condition) {
		if (logger.isDebugEnabled()) {
			logger.debug("setCondition(String) - start");
		}
		this.condition = condition;

		if (logger.isDebugEnabled()) {
			logger.debug("setCondition(String) - end");
		}
	}

	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end");
		}
		return "SUCESS";
	}

	public String doList(){
		if (logger.isDebugEnabled()) {
			logger.debug("doList() - start");
		}
		TypeDefineDAO dao=new TypeDefineDAO();
		this.objects=dao.findByCondition(condition);
		dao.getSession().close();

		if (logger.isDebugEnabled()) {
			logger.debug("doList() - end");
		}
		return "list";
	}

	public String doAdd(){
		if (logger.isDebugEnabled()) {
			logger.debug("doAdd() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("doAdd() - end");
		}
		return "add";
	}

	public String doEdit(){
		if (logger.isDebugEnabled()) {
			logger.debug("doEdit() - start");
		}
		TypeDefineDAO dao=new TypeDefineDAO();
		this.typedefine=dao.findById(id);
		dao.getSession().close();
		this.sedObj();
		
		if (logger.isDebugEnabled()) {
			logger.debug("doEdit() - end");
		}
		if(this.TYPE_SQL.equals(this.tmpl)){
			return "edit";
		}else{
			
			try {
//			FileInputStream fin=new FileInputStream(file);
				String path=this.typedefine.getMobilefilename();
				File file=new File(path);
				FileReader fread=new FileReader(file);
				BufferedReader reader = new BufferedReader(fread);
				String line=null;
				StringBuffer bf=new StringBuffer();
				line=reader.readLine();
				int t=0;
				while(true){
					if(null!=line && !"".equals(line.trim())){
						bf.append(line).append(";");
					}else{
						t++;
						if(t>20)break;
					}
					line=reader.readLine();
				}
				this.toMobiles=bf.toString();
			} catch (Exception e) {
				
				e.printStackTrace();
			}
			
			return "edit1";
		}
	}

	public String doSave(){
		if (logger.isDebugEnabled()) {
			logger.debug("doSave() - start");
		}
		this.buildObj();
		if(null==this.typedefine.getId()){
//		insert a new object
			TypeDefineDAO dao=new TypeDefineDAO();
		//gave the object id by database 
				
			if(this.TYPE_SQL.equalsIgnoreCase(this.tmpl)){
				
			}else{
				this.setMobile(this.toMobiles);
				this.setMobile(this.toPersons);
				this.setMobileFile(this.mobilefile);
				String mobilepath=writeFile();
				this.typedefine.setMobilefilename(mobilepath);
			}
			dao.add(typedefine);
			dao.getSession().close();
		}else{
//		update the object
			TypeDefineDAO dao=new TypeDefineDAO();
			String filep=this.typedefine.getMobilefilename();
			if(null!=filep){
				File file=new File(filep);
				file.delete();
			}
			this.setMobile(this.toMobiles);
			this.setMobile(this.toPersons);
			this.setMobileFile(this.mobilefile);
			String mobilepath=writeFile();
			this.typedefine.setMobilefilename(mobilepath);
			dao.update(typedefine);
			dao.getSession().close();
		}
		

		if (logger.isDebugEnabled()) {
			logger.debug("doSave() - end");
		}
		return "list";
	}
	
	private String writeFile(){
		
		String filepath="mobiles"+new Date().getTime()+".txt";
		String fullp=this.request.getRealPath("/");
		fullp+="mobiles/";
		File dir=new File(fullp);
		if(!dir.exists()){
			dir.mkdirs();
		}
		fullp+=filepath;
		
		File file=new File(fullp);
		
		try {
			if(!file.exists()){
				file.createNewFile();
			}
			
			StringBuffer bf=new StringBuffer();
			if(null!=this.mobiles){
				String mobile=null;
				Iterator it=this.mobiles.iterator();
				while(it.hasNext()){
					mobile=(String)it.next();
					bf.append(mobile).append("\r\n");
					mobile=null;
				}
				
			}
			 FileOutputStream writer=new  FileOutputStream(file);
				writer.write(bf.toString().getBytes("UTF-8"));
				writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return fullp;
	}
	
	public void setMobile(String mobilesStr){
		if(null!=mobilesStr && !"".equals(mobilesStr.trim())){
			String[] mobils=null;
			if(-1!=mobilesStr.indexOf(";")){
				mobils=mobilesStr.split(";");
			}else if(-1!=mobilesStr.indexOf("||")){
				mobils=mobilesStr.split("||");
			}else{
				if(null!=mobilesStr && !"".equalsIgnoreCase(mobilesStr.trim()))
					this.mobiles.add(mobilesStr);
			}
			if(null==mobils)return ;
			int l=mobils.length;
			for(int i=0;i<l;i++){
				String tmp=mobils[i];
				if(null!=tmp && !"".equalsIgnoreCase(tmp.trim()))
					this.mobiles.add(mobils[i]);
			}
			
		}
	}
	
	public void setMobileFile(File file){
		if(null!=file && file.exists()){
			
			try {
//				FileInputStream fin=new FileInputStream(file);
				FileReader fread=new FileReader(file);
				BufferedReader reader = new BufferedReader(fread);
				String line=null;
				
				line=reader.readLine();
				int t=0;
				while(true){
					if(null!=line && !"".equals(line.trim())){
						this.mobiles.add(line);
					}else{
						t++;
						if(t>20)break;
					}
					line=reader.readLine();
				}
			} catch (Exception e) {
				
				e.printStackTrace();
			}
		}
		
	}
	
	public void setUsers(String userId){
		if(null!=userId && !"".equals(userId)){
			if(userId.endsWith(";")){
				userId=userId.substring(0, userId.length()-1);
			}
			UserlistDAO dao=new UserlistDAO();
			List users=dao.findByCondition("id in ("+userId+")");
			if(null!=users){
				Iterator it=users.iterator();
				Userlist user=null;
				while(it.hasNext()){
					user=(Userlist)it.next();
					this.mobiles.add(user.getTelphone());
				}
			}
		}
		
	}

	public String doDetail(){
		if (logger.isDebugEnabled()) {
			logger.debug("doDetail() - start");
		}
			TypeDefineDAO dao=new TypeDefineDAO();
		this.typedefine=dao.findById(id);
		dao.getSession().close();
		this.sedObj();;

		if (logger.isDebugEnabled()) {
			logger.debug("doDetail() - end");
		}
		return "detail";
	}

	public String doDelete(){
		if (logger.isDebugEnabled()) {
			logger.debug("doDelete() - start");
		}

		TypeDefineDAO dao=new TypeDefineDAO();
		dao.deleteObjs(this.ids);

		if (logger.isDebugEnabled()) {
			logger.debug("doDelete() - end");
		}
		return "list";
	}

	private void buildObj(){
		if (logger.isDebugEnabled()) {
			logger.debug("buildObj() - start");
		}
		this.typedefine=new TypeDefine();
		this.typedefine.setId(this.id);
		this.typedefine.setContent(this.content);
		this.typedefine.setSqlstr(this.sqlstr);
		this.typedefine.setContentFrom(this.contentFrom);
		this.typedefine.setStartDate(this.startDate);
		this.typedefine.setLoopType(this.loopType);
		this.typedefine.setTypeName(this.typeName);
		this.typedefine.setDbLocation(this.dbLocation);
		this.typedefine.setDbType(this.dbType);
		this.typedefine.setDbDirver(this.dbDirver);
		this.typedefine.setDbUser(this.dbUser);
		this.typedefine.setDbPassword(this.dbPassword);
		this.typedefine.setStatus(this.status);
		this.typedefine.setTmpl(this.tmpl);
		this.typedefine.setMobilefilename(this.mobilefilename);
		if (logger.isDebugEnabled()) {
			logger.debug("buildObj() - end");
		}
	}
	private void sedObj(){
		if (logger.isDebugEnabled()) {
			logger.debug("sedObj() - start");
		}

		if(null!=this.typedefine){
			this.id=this.typedefine.getId();
			this.content=this.typedefine.getContent();
			this.sqlstr=this.typedefine.getSqlstr();
			this.contentFrom=this.typedefine.getContentFrom();
			this.startDate=this.typedefine.getStartDate();
			this.loopType=this.typedefine.getLoopType();
			this.typeName=this.typedefine.getTypeName();
			this.dbLocation=this.typedefine.getDbLocation();
			this.dbType=this.typedefine.getDbType();
			this.dbDirver=this.typedefine.getDbDirver();
			this.dbUser=this.typedefine.getDbUser();
			this.dbPassword=this.typedefine.getDbPassword();
			this.status=this.typedefine.getStatus();
			this.tmpl=this.typedefine.getTmpl();
			this.mobilefilename=this.typedefine.getMobilefilename();
		}

		if (logger.isDebugEnabled()) {
			logger.debug("sedObj() - end");
		}
	}
	public String getTmpl() {
		return tmpl;
	}
	public void setTmpl(String tmpl) {
		this.tmpl = tmpl;
	}
	public java.lang.String getTitle() {
		return title;
	}
	public void setTitle(java.lang.String title) {
		this.title = title;
	}
	public String getToMobiles() {
		return toMobiles;
	}
	public void setToMobiles(String toMobiles) {
		this.toMobiles = toMobiles;
	}
	public String getToPersons() {
		return toPersons;
	}
	public void setToPersons(String toPersons) {
		this.toPersons = toPersons;
	}
	public File getMobilefile() {
		return mobilefile;
	}
	public void setMobilefile(File mobilefile) {
		this.mobilefile = mobilefile;
	}
	public List getMobiles() {
		return mobiles;
	}
	public void setMobiles(List mobiles) {
		this.mobiles = mobiles;
	}
	
	public static void main(String[]args){
//		String p
		System.out.println(new Date().getTime());
	}
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.request=arg0;
	}
	public String getMobilefilename() {
		return mobilefilename;
	}
	public void setMobilefilename(String mobilefilename) {
		this.mobilefilename = mobilefilename;
	}
}